package com.fbinggun.fattribute.listener;

import com.fbinggun.fattribute.attribute.*;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

import java.util.HashMap;
import java.util.Map;

public class DefenseListener implements Listener {


    @EventHandler(priority = EventPriority.HIGH)
    public void attacke(EntityDamageByEntityEvent event){
        if(event.getEntity() instanceof LivingEntity) {
            EntityData entityData = DataManage.getData((LivingEntity) event.getEntity());
            Map<IAttribute,Double> damageMap = new HashMap<>();
            double damage=0;
            for(IAttribute attribute: AttributeManage.getAttributeMap().values()){
                if(attribute instanceof ADefense){
                    ADefense defense = (ADefense) attribute;
                    if(entityData.getValue(defense.getKey())!=0) {
                        damageMap.put(defense, defense.getDefense(entityData,event));
                    }
                }
            }
            if(damageMap.size()!=0){
                for(IAttribute attribute:damageMap.keySet()){
                    damage=damageMap.get(attribute)+damage;
                }
            }

            event.setDamage(event.getDamage()-damage);

        }
    }

}
